/**
 * 
 */
package tau.adx.common.enteties.ranking.impl;

import gnu.trove.map.hash.TObjectIntHashMap;
import tau.adx.common.adn.AdNetwork;
import tau.adx.common.enteties.ranking.RankingAgency;
import tau.adx.common.messages.auction.AuctionWon;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * @author greenwald
 * 
 */
public class SimpleRankingAgency implements RankingAgency {

	/**
	 * Main {@link AdNetwork} ranking map. Each {@link AdNetwork} is ranked
	 * according to the number of auctions it won.
	 */
	TObjectIntHashMap<AdNetwork> rankingMap = new TObjectIntHashMap<>();

	/**
	 * @param auctionWon
	 *            {@link AuctionWon} notification from the {@link EventBus}.
	 */
	@Override
	@Subscribe
	public void bidWon(AuctionWon auctionWon) {
		synchronized (rankingMap) {
			rankingMap.adjustOrPutValue(auctionWon.getAdNetwork(), 1, 1);
		}
	}

	/**
	 * @see tau.adx.common.enteties.ranking.RankingAgency#clear()
	 */
	@Override
	public void clear() {
		rankingMap.clear();
	}

	/**
	 * @see tau.adx.common.enteties.ranking.RankingAgency#getRanking(tau.adx.common.adn.AdNetwork)
	 */
	@Override
	public Double getRanking(AdNetwork adNetwork) {
		return (double) rankingMap.get(adNetwork);
	}
}
